﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="HQWeb.Procurement.Edit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>采购信息</title>
    <link href="../Styles/Site.css" rel="Stylesheet" type="text/css" />
    <script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" language="javascript">
//        function checkNum() {
//            var reg = /^[0-9]+$/;
//            var shu = document.getElementById("txtnum").value;
//            if (!reg.test(shu)) {
//                alert("申请数量必须是整数");
//                document.getElementById("txtnum").value = "";
//                return false;
//            }
//        }


              Array.prototype.contains = function(obj) {
                            var i = this.length;
                            while (i--) {
                                if (this[i] === obj) {
                                    return true;
                                }
                            }
                            return false;
                        }

           function AddMultiMaterial(vItemID, vItemNameHid) 
           {
            var vIDValue = document.getElementById(vItemID);
            var vNameValueHid = document.getElementById(vItemNameHid);
            
            var vURL,vHeight,vWidth;
            vURL = "../Resources/MultiMaterialList/SelectFrame.aspx?itemIDValue=" + vIDValue.value;
            vHeight = "525px";
            vWidth = "875px";
                
            var vReturn = window.showModalDialog(vURL,"元素选择","dialogWidth:"+vWidth+";dialogHeight:"+vHeight+";center:yes;help=no;resizable:no;status:no;scroll=no");
            if(vReturn==null)
            {}
            else
            {
                if(vReturn.length ==2)
                {
                    vIDValue.value = vReturn[0];
	                vNameValueHid.value = vReturn[1];
                    var NameArray = vNameValueHid.value.split(",");
                    var IDArray = new Array();
                    IDArray = vIDValue.value.split(",");
                    var exist = document.getElementById("hidMaterialIDList").value;
                    var shortexist = exist.substring(1,exist.length-1);
                    var existArray = shortexist.split(",");
                    if(existArray != ""){
                        for(var j=0;j<existArray.length;j++){
                            if(!IDArray.contains(existArray[j])){
                                var delnode = document.getElementById(existArray[j]).value;
                                DeleteNode(delnode);
                            }
                        }
                    }
                    for(var i=0;i<IDArray.length;i++){
                       AddMaterial(IDArray[i], NameArray[i]); 
                    }
	            }
            }
        }

       function AddMaterial(strid, strname) {
            //var str = window.showModalDialog("../Resources/MaterialFrame.htm", " ", "dialogWidth=800px;dialogHeight=400px");
             if (strid != "") {
                var materialList = document.getElementById("hidMaterialIDList").value;
                if (materialList.indexOf(',' + strid + ',') >= 0) {
                    //alert("添加重复项目！");
                }
                else {
                    if (materialList != null && materialList != "") {
                        document.getElementById("hidMaterialIDList").value += strid + ",";
                    } else {
                        document.getElementById("hidMaterialIDList").value = "," + strid + ",";
                    }
                    var Count = document.getElementById("hidCount").value;
                    var strHtml = "";
                    strHtml = strHtml + '  <table width="58%" id=\"table' + Count + '\">';
                    strHtml = strHtml + "<tr>";
                    strHtml = strHtml + "<td align=\"right\" style=\"width: 10%\">";
                    strHtml = strHtml + " 元素名称：";
                    strHtml = strHtml + "</td><td align=\"left\" style=\"width: 10%\">"
                    strHtml = strHtml + "<input type=\"text\" readonly=\"readonly\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strname + "\" id=\"n" + Count + "\" />";
                    strHtml = strHtml + "</td><td align=\"right\" style=\"width: 10%\">";
                    strHtml = strHtml + "申请数量：";
                    strHtml = strHtml + "</td><td align=\"left\" style=\"width: 10%\">";
                    strHtml = strHtml + "<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + Count + "\" />";
        //            strHtml = strHtml + "</td><td align=\"right\" style=\"width: 10%\">";
        //            strHtml = strHtml + "库存量：";
        //            strHtml = strHtml + "</td><td align=\"left\">";
        //            strHtml = strHtml + "<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\"  value=\"" + str.num + "\" readonly=\"readonly\" id=\"c" + Count + "\" />";
        //            strHtml = strHtml + "</td>";
                    strHtml = strHtml + "<td align=\"right\" style=\"width: 10%\">";
                    strHtml = strHtml + "<input type=\"button\" id=\"del" + Count + "\" value=\"删除\"  onclick=\"DeleteNode('table" + Count + "')\"/>";
                    strHtml = strHtml + "<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" style=\"display: none\" value=\"" + strid + "\" id=\"p" + Count + "\" />";
                    strHtml = strHtml + " <input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"e" + Count + "\" style=\"display: none\" />";
                    strHtml = strHtml + " <input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"" + strid + "\" value=\"table" + Count +"\" style=\"display: none\" />";
                    strHtml = strHtml + "</td>";
                    strHtml = strHtml + "</tr>";
                    strHtml = strHtml + "</table>";

                    document.getElementById("hidCount").value = parseInt(Count, 10) + 1;
                    document.getElementById("Addtable").innerHTML = document.getElementById("Addtable").innerHTML + strHtml;
             }
         }
     }

        //JS 删除节点
        function DeleteNode(nodeID) {
            var varType = document.getElementById("hidType").value;
            if (varType == "edit") {
                var varDelID = "e" + nodeID.substr(5);
                varDelID = document.getElementById(varDelID).value;
                HQWeb.LibarySys.LibraryServer.DelPurchaseMaterials(varDelID, DelResout, failedCallback);
            }
            //去掉检测重复项中的id
            var materialList = document.getElementById("hidMaterialIDList").value;
            var materialID = "p" + nodeID.substr(5);
            var materialIDValue = "," + document.getElementById(materialID).value + ",";
            materialList = materialList.replace(materialIDValue, ",");
            document.getElementById("hidMaterialIDList").value = materialList;
            var IDList = "," + document.getElementById("txtUserIdHid").value + ",";//去掉元素列表中的id
            IDList = IDList.replace(materialIDValue, ",");
            //IDList = IDList.substring(1);
            IDList = IDList.substring(1,IDList.length-1);
            document.getElementById("txtUserIdHid").value = IDList;
            var NameList = document.getElementById("txtUserNameHid").value;
            var NameID = "n" + nodeID.substr(5);
            var NameIDValue = "," + document.getElementById(NameID).value + ",";
            NameList = NameList.replace(NameIDValue, ",");
            //NameList = NameList.substring(1);
            NameList = NameList.substring(1,NameList.length-1);
            document.getElementById("txtUserNameHid").value = NameList;
            var trnode = document.getElementById(nodeID);
            trnode.parentNode.removeChild(trnode);
        }

        //删除回调函数
        function DelResout(resout) {
            if (resout != "") {
              if (resout == "SessionTimeout") {
                    top.location.href = '../Login/Login.aspx';
                }
                else {
                    alert("resout");
                }
            }
        }

        function AddData() {
            document.getElementById("apply").disabled = true;     //不可保存
            var nameInput = document.getElementById("applytitel");
            if (nameInput.value == "") {
                alert("采购标题不能为空！");
                nameInput.focus();
                document.getElementById("apply").disabled = false;     //可保存
                return;
            }
            var endtime = document.getElementById("endtime");
            if(endtime.value == ""){
                alert("截止日期不能为空！");
                endtime.focus();
                document.getElementById("apply").disabled = false;
                return;
            }
            var Count = document.getElementById("hidCount").value;
            for (var i = 0; i < Count; i++) {
                var idTemp1 = "s" + i;
                var inputTemp1 = document.getElementById(idTemp1);
                if (inputTemp1 != null && inputTemp1 != undefined) {
                    //                 var idTemp2 = "c" + i;
                    //                 var inputTemp2 = document.getElementById(idTemp2).value;
                    if (inputTemp1.value == "") {
                        alert("申请数量不能为空！");
                        document.getElementById(idTemp1).focus();
                        document.getElementById("apply").disabled = false;
                        return;
                    }
                    else {
                        if (!checkRate(inputTemp1)) {
                            alert("所需数必须是整数");
                            document.getElementById(idTemp1).value = "";
                            document.getElementById(idTemp1).focus();
                            document.getElementById("apply").disabled = false;
                            return;
                        } 
                    }
                }
            }
            var varType = document.getElementById("hidType").value;
            var PurchaseModel = new Object();
            if (varType == "edit") {
                PurchaseModel.ID = document.getElementById("hidPid").value;
            }
            PurchaseModel.TITLE = document.getElementById("applytitel").value;
            PurchaseModel.END_TIME = document.getElementById("endtime").value;
            //PurchaseModel.APPLICANT = <%=Session["USER_ID"] %>;
            //PurchaseModel.APPLICANT_NAME = <%=Session["USER_NAME"] %>;
            PurchaseModel.REMARK = document.getElementById("Remark").value;
            if(<%=Session["BRANCH_ID"]%> != null)
            {
                PurchaseModel.BRANCH_ID = <%=Session["BRANCH_ID"] %>;
            }
            var info = GeArrList(varType);
            if (info[0] != "") {
                alert(info[0]);
                document.getElementById("apply").disabled = false;     //可保存
                return;
            } else {
                if (varType == "edit") {
                    HQWeb.LibarySys.LibraryServer.EditPurchase(PurchaseModel, info[1], SaveResout, failedCallback);
                } else {
                    HQWeb.LibarySys.LibraryServer.AddPurchase(PurchaseModel, info[1], SaveResout, failedCallback);
                }
            }
            document.getElementById("apply").disabled = false;     //可保存
        }

        //保存返回结果
        function SaveResout(resout) {
            if (resout == "") {
                //Response.Redirect("ApplyList.aspx");
                document.getElementById("btnCalcel").click();
                //alert("保存成功！");
                //document.getElementById("apply").disabled = true;
            } else {
              if (resout == "SessionTimeout") {
                    top.location.href = '../Login/Login.aspx';
                }
                else {
                    alert("resout");
                }
            }
        }
        //获得元素事件
        function GeArrList(type) {
            var objArry = new Array();
            var Count = document.getElementById("hidCount").value;
            var varPID = document.getElementById("hidPid").value;
            for (var i = 0; i < Count; i++) {
                var idTemp = "p" + i;
                var inputTpem = document.getElementById(idTemp);
                if (inputTpem != null && inputTpem != undefined) {
                    var obj = new Object();
                    if (inputTpem.value == "") {
                        var alter = "元素种类必选！";
                        return [alter, null];
                    } else {
                        obj.MATERIAL_ID = inputTpem.value;
                        idTemp = "s" + i;
                        inputTpem = document.getElementById(idTemp);
                        if (inputTpem.value != "") {
                            obj.NUM = inputTpem.value;
                        }
                        idTemp = "n" + i;
                        inputTpem = document.getElementById(idTemp);
                        if (inputTpem.value != "") {
                            obj.MATERIAL_NAME = inputTpem.value;
                        }
                        if (type == "edit") {
                            idTemp = "e" + i;
                            if (document.getElementById(idTemp).value != "") {
                                obj.ID = document.getElementById(idTemp).value;
                            } else {
                                obj.ID = 0;
                            }

                            obj.PURCHASE_ID = varPID;
                        }
                    }
                    objArry.push(obj);
                }

            }
            return ["", objArry]
        }

        //客户端异常处理
        function failedCallback(error) {
            alert(error.get_message());
        }

        function checkRate(input) {
            var value = input.value;
            if (value != "") {
                var re = /^[0-9]+$/;
                if (!re.test(value)) {
                    //alert("请输入数字");
                    //input.value = "";
                    //input.focus();
                    return false;
                }
                else{
                    return true;
                }
            }
//             else {
//                  alert("申请数量不能为空");
//                  input.focus();
//            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="../LibarySys/LibraryServer.asmx" />
        </Services>
    </asp:ScriptManager>
    <div>
        <table align="center" width="98%">
            <tr>
                <td colspan="4" class="title">
                    <asp:Label runat="server" ID="labTitle" Text="采购信息表单"> </asp:Label>
                </td>
            </tr>
            <tr>
                <td align="right" style="width: 15%">
                    采购标题：
                </td>
                <td class="content" colspan="3">
                    <input type="text" id="applytitel" runat="server" /><span style="color: Red">*</span>
                   <%-- <asp:TextBox ID="txtpurchase" runat="server" MaxLength="20"></asp:TextBox><span style="color: Red">*</span><asp:RequiredFieldValidator
                        ID="RequiredFieldValidator2" runat="server" ErrorMessage="采购标题不能为空！" ControlToValidate="txtpurchase"></asp:RequiredFieldValidator>
                    <font style="color: Red;">&nbsp;&nbsp;<label id="Msg"></label></font>--%>
                </td>
            </tr>
            <tr>
                <td align="right" style="width: 20%">
                    选择采购元素：
                </td>
                <td style="width: 30%">
                          &nbsp;&nbsp; &nbsp;&nbsp;<input type="button" runat="server" value="选 择" id="ipt_select"
                                    onclick="AddMultiMaterial('txtUserIdHid','txtUserNameHid');" />
                </td>
                <td style="width: 10%" align="right">
                    截止时间：
                </td>
                <td >
                    <input id="endtime" runat="server" type="text" onfocus="WdatePicker({minDate:'%y-%M-{%d}'})"/>
                    <img onclick="WdatePicker({el:'endtime'})" src="../My97DatePicker/skin/datePicker.gif"
                        align="middle" width="16" height="22"><span style="color: Red">*</span>
                </td>
            </tr>
           <tr>
                <td colspan="4">
                    <div align="center">
                        <div id="Addtable" runat="server">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td align="right">
                    备 注：
                </td>
                <td colspan="3">
                    <%--<asp:TextBox ID="txtremark" TextMode="MultiLine" runat="server" MaxLength="50" Width="70%"></asp:TextBox>--%>
                    <input type="text" id="Remark" runat="server" style="width:50%;" />
                </td>
            </tr>
            <tbody id="allow" runat="server" style="display: none;">
                <tr>
                    <td align="right" style="width: 20%">
                        <asp:Label runat="server" ID="lblapprover" Text="审批人："></asp:Label>
                    </td>
                    <td colspan="3">
                        <asp:Label runat="server" Width="70%" ID="Approver"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="width: 20%">
                        <asp:Label runat="server" ID="lbl_notation" Text="批 注："></asp:Label>
                    </td>
                    <td colspan="3">
                        <asp:Label runat="server" Width="70%" ID="Notation"></asp:Label>
                    </td>
                </tr>
            </tbody>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td colspan="3"> &nbsp; &nbsp; &nbsp; &nbsp;
                    <%--<asp:Button ID="btnSend" runat="server" Text="提 交" CommandName="add" OnClick="btnSend_Click" />--%>
                    <input type="button" value="提 交" id="apply" onclick="AddData()" />
                    &nbsp;
                    <input id="btnCalcel" value="返 回" type="button" onclick="location='ApplyList.aspx?userid=<%=Request["userid"]%>'" />
                </td>
            </tr>
        </table>
    </div>
    <div style="display: none">
         <input id="txtUserNameHid" runat="server" title="人员姓名隐藏" style="visibility: hidden" />
        <input id="txtUserIdHid" runat="server" title="人员ID隐藏" style="visibility: hidden" />
        <asp:HiddenField ID="hidPid" runat="server" />
        <asp:HiddenField ID="hidType" runat="server" Value="add" />
        <asp:HiddenField ID="hidCount" runat="server" Value="0" />
        <asp:HiddenField ID="hidMaterialIDList" runat="server" />
    </div>
    </form>
</body>
</html>
